// get 在req.query, post在req.body
const jwtUtil = require('../utils/jwtUtils')
const fs = require('fs')
const moment = require('moment')

module.exports = class AdminDao extends require('../model/admin_mod') {
  // 登录请求
  static async login(req, res) {
    const { username, password } = req.body
    const loginData = await this.loginAdminMod(username, password)

    if (loginData.length > 0) {
      const token = jwtUtil.sign(
        {
          id: loginData[0].id,
          username: loginData[0].username,
          password: loginData[0].password,
        },
        global.globalKey,
        3600 * 12,
      )
      res.json({
        username: loginData[0].username,
        id: loginData[0].id,
        status: 200,
        statusText: 'ok',
        token,
      })
    } else {
      res.json({
        status: 500,
        statusText: '账号或密码错误~~',
      })
    }
  }

  // 根据加密的token解析用户信息
  static async getAdminDataByToken(req, res) {
    const results = await jwtUtil.verifysync(req.query.token, global.globalKey)
    res.json({
      status: 200,
      statusText: 'ok',
      username: results.username,
      id: results.id,
    })
  }

  // 获取标签分类
  static async getTagInfo(req, res) {
    const tagList = await this.getTagInfoMod()
    res.json({
      status: 200,
      statusText: 'ok',
      tagList,
    })
  }

  // 发布或更新文章
  static async updateArticle(req, res) {
    const data = await this.updateArticleMod(req.body)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 获取所有文章
  static async getArticleList(req, res) {
    const data = await this.getArticleListMod()
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 删除文章
  static async delArticle(req, res) {
    const data = await this.delArticleMod(req.body.id)
    res.json({
      status: 200,
      statusText: 'ok',
    })
  }
  // 根据文章id查询文章信息
  static async getArticleById(req, res) {
    const data = await this.getArticleByIdMod(req.body.id)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }
  // 获取每种文章的数量
  static async getDiffTypeArticleInfo(req, res) {
    const data = await this.getDiffTypeArticleInfoMod()
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 获取近七日的访客数
  static async getNearWeekVisitor(req, res) {
    const data = await this.getNearWeekVisitorMod()
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 获取评论列表
  static async getCommentList(req, res) {
    const data = await this.getCommentListMod(req.query.pageNum, req.query.pageSize)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 审核评论状态
  static async changeCommentStatus(req, res) {
    const data = await this.changeCommentStatusMod(req.body.id, req.body.status)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }

  // 修改文章状态
  static async changeArticleStatus(req, res) {
    const data = await this.changeArticleStatusMod(req.body.id, req.body.status)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }
  // 删除评论
  static async deleteCommentById(req, res) {
    const data = await this.deleteCommentByIdMod(req.body.id)
    res.json({
      status: 200,
      statusText: 'ok',
      data,
    })
  }
  // 上传markdown图片
  static async uploadImage(req, res) {
    let base64 = req.body.base64
    const handleDir = (dir) => {
      return new Promise((resovle, reject) => {
        const exists = fs.existsSync(dir)
        if (exists === false) {
          fs.mkdirSync(dir)
        }
      })
    }
    const path = 'public/images/' + Date.now() + '.png'
    const date = moment(new Date()).format('YYYY-MM').replace(/-/gi, '')
    const day = moment(new Date()).format('DD')
    let dir = `public/images/` + date
    // 创建文件
    handleDir(dir)
    dir += '/' + day
    handleDir(dir)
    base64 = base64.replace(/^data:image\/\w+;base64,/, '') // 去掉图片base64码前面部分data:image/png;base64
    const dataBuffer = new Buffer.from(base64, 'base64') // 把base64码转成buffer对象，
    dir += '/' + Date.now() + '.png'
    fs.writeFile(dir, dataBuffer, () => '')

    res.json({
      status: 200,
      statusText: 'Ok',
      path: dir.replace(/public/gi, ''),
    })
  }
}
